package fr.mywiki.view.web.struts.action.entry;

import java.rmi.RemoteException;
import java.sql.SQLException;

import javax.ejb.FinderException;
import javax.ejb.ObjectNotFoundException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import fr.mywiki.business.entry.EntryLite;
import fr.mywiki.business.entry.EntryManager;
import fr.mywiki.business.versioning.VersionLite;
import fr.mywiki.model.ejbAdapter.DataController;
import fr.mywiki.view.web.struts.action.ListAction;
import fr.mywiki.view.web.struts.action.version.VersionListAction;
import fr.mywiki.view.web.struts.form.entry.EntryPageForm;

/**
 * Displays an entry on the page.
 * 
 * @author tlombard
 */
public class EntryPageAction extends DispatchAction {
	/**
	 * The identifier of the entry is stored in the request attributes with this attribute name.
	 * The identifier must be under the form <code>String</code> object.
	 */
	public static final String ENTRY_ID = ListAction.ELEMENT_ID;

	/**
	 * Inits and displays the page with the correct entry on it.
	 * 
	 * @throws RemoteException if the entry corresponding to the identifier given in request
	 *             parameter could not be found.
	 */
	public ActionForward displayEntry(ActionMapping mapping,
      ActionForm actionForm, HttpServletRequest request,
      HttpServletResponse arg3) throws RemoteException, FinderException, SQLException {
		/* The identifier of the entry is given by a request attribute */
		EntryPageForm form = (EntryPageForm) actionForm;

		Long entryId = (Long) request.getAttribute(EntryPageAction.ENTRY_ID);
		Long versionId = (Long) request.getAttribute(VersionListAction.ENTRY_ID);
		
		if (entryId != null)
			form.init((EntryLite) DataController.getValue(entryId,
					EntryLite.TYPE_ENT),request);
		else if (versionId != null)
			form.init((VersionLite) DataController.getValue(versionId,
					VersionLite.TYPE_ENT),request);

		return mapping.findForward("display");
	}

	/**
	 * Forwards to the edition page of the current version.
	 */
	public ActionForward editEntry(ActionMapping mapping, ActionForm actionForm,
			HttpServletRequest request, HttpServletResponse arg3) {
		EntryPageForm form = (EntryPageForm) actionForm;
		request.setAttribute(EditEntryAction.VERSION_ID,
				form.getVersionId().toString());
		return mapping.findForward("edit");
	}
	
	/**
	 * Forwards to the edition/creation page of the entry whose title is given
	 * by the entryTitle variable.
	 * 
	 * TODO Useful ?
	 */
	public ActionForward createEntry(ActionMapping mapping, ActionForm actionForm,
			HttpServletRequest request, HttpServletResponse arg3) {
		EntryPageForm form = (EntryPageForm) actionForm;
		request.setAttribute(EditEntryAction.ENTRY_TITLE,
				form.getEntryTitle());
		return mapping.findForward("create");
	}
	
	/**
	 * Forwards to another entry's consultation whose title is given by the
	 * entryTitle variable.
	 */
	public ActionForward seeEntry(ActionMapping mapping, ActionForm actionForm,
			HttpServletRequest request, HttpServletResponse arg3) {
		EntryPageForm form = (EntryPageForm) actionForm;
		request.setAttribute(EntryPageAction.ENTRY_ID,form.getEntry().getId());
		return mapping.findForward("displayEntry");
	}
	
	/**
	 * Displays the list of versions of this entry.
	 */
	public ActionForward seeHistory(ActionMapping mapping, ActionForm actionForm,
			HttpServletRequest request, HttpServletResponse arg3) {
		EntryPageForm form = (EntryPageForm) actionForm;

		request.setAttribute(VersionListAction.ENTRY_ID,form.getEntry().getId());
		return mapping.findForward("history");
	}
	
	/**
	 * Deletes the current entry.
	 */
	public ActionForward deleteEntry(ActionMapping mapping,
			ActionForm actionForm, HttpServletRequest request,
			HttpServletResponse arg3) throws ObjectNotFoundException, RemoteException {
		EntryPageForm form = (EntryPageForm) actionForm;
		EntryManager.deleteEntry(form.getEntry());

		return mapping.findForward("home");
	}
}
